import {
  conbineHolderList
} from '../../config/apiServe/new-rpc'
export default {
  props: {
    infos: {},
    addr: '',
    totalHolder: 0,
    pages: {
      type: Object,
      default: () => {
        return {
          currentPage: 1,
          pageSize: 10,
          total: 10
        }
      }
    }
  },
  data() {
    return {
      Holders: [],
      holderParams: [{
        page: {
          number: 1,
          size: 10
        },
        "sort": [{
          "key": "balance",
          "ascending": false
        }],
        range: [{
          key: 'balance',
          gt: 0
        }],
        "match": [{
          "value": this.$route.query.TokenName,
          "key": "asset_symbol"
        }]
      }]
    }
  },
  computed: {
    totalNum() {
      return this.infos.total
    }
  },
  watch: {
    addr(val) {
      if (!JSON.stringify(this.holderParams[0].match).includes('"key":"address"'))
        this.holderParams[0].match.push({
          key: "address",
          value: val
        })
      else {
        this.holderParams[0].match[1].value = val
      }
      this.pages.total = 1
      this.init()
    },
    pages: {
      handler(newPages) {
        this.holderParams[0].page.number = newPages.currentPage
        this.holderParams[0].page.size = newPages.pageSize
        this.getHolderList()
      },
      deep: true
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getHolderList()
    },
    getHolderList() {
      // this.holderParams[0].tokens[0] = this.$route.query.TokenName
      conbineHolderList(this.holderParams).then(res => {
        if (res.error === null) {
          this.Holders = res.result || []
          this.Holders.forEach((item, index) => {
            item.index = index + (this.pages.currentPage - 1) * this.pages.pageSize + 1
          })
        }
      })
    }
  }
}